package com.foodmarket.service.impl;

import com.foodmarket.mapper.FoodMapper;
import com.foodmarket.pojo.FoodItems;
import com.foodmarket.service.FoodService;
import com.foodmarket.utils.ThreadLocalUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.Map;

@Service
public class FoodServiceImpl implements FoodService {

    @Autowired
    private FoodMapper foodMapper;
    @Override
    public void add(FoodItems foodItems)
    {
        Map<String,Object>map=ThreadLocalUtil.get();
        System.out.println("ThreadLocal contains: " + map);
        Integer sellerId=(Integer)map.get("id");
        foodItems.setSellerId(sellerId);

        foodMapper.addFood(foodItems);
    }

    @Override
    public FoodItems findByFoodName(String foodName) {
        FoodItems foods = foodMapper.findByFoodName(foodName);
        return foods;
    }

    @Override
    public List<FoodItems> findAllFood(int sellerId)
    {
        List<FoodItems> foods=foodMapper.findAllFood(sellerId);
        return foods;
    }

    @Override
    public List<FoodItems> findAllFoodItems()
    {
        Map<String,Object>map=ThreadLocalUtil.get();
        Integer buyerId=(Integer)map.get("id");
        System.out.println(buyerId);
        List<FoodItems> foodItems=foodMapper.findAllFoodItems(buyerId);
        return foodItems;
    }

    @Override
    public void updateFood(FoodItems foodItems) {
        foodMapper.updateFood(foodItems);
    }

    @Override
    public void deleteByFoodName(String foodName) {
        foodMapper.deleteFood(foodName);
    }

    @Override
    public  FoodItems findByFoodId(Integer foodId){
        System.out.println(foodId);
        FoodItems foods=foodMapper.findByFoodId(foodId);
        System.out.println(foods);
        return foods;
    }

}
